package com.zxl.controller;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.swing.FilePane;

import java.io.IOException;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${fastpath}")
    private String url;

    @Autowired
    private FastFileStorageClient fileStorageClient;

    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        String filename = file.getOriginalFilename();
        String exName = FilenameUtils.getExtension(filename);
        String path = url;
        try {
            StorePath storePath = fileStorageClient
                    .uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), exName, null);
            String fullPath = storePath.getFullPath();
            path += fullPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("file:"+path);
        return path;
    }
}
